#!/usr/bin/env python 2.7
# -*- coding: utf-8 -*-
# @Time      :2019/4/9
# @Author    :breeze
# @Email     :wangbaoshan@bbdservice.com
# @File      :IC01_Past1MonQueryCnt
# @Software  :PyCharm


'''eof
name:最近1月内的查询次数
code:IC01_Past1MonQueryCnt_2D
tableName:
columnName:
groups:个人征信模块
dependencies:IC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:
eof'''

import json
import sys
import re
import datetime

reload(sys)
sys.setdefaultencoding('utf-8')


def getTable(data, tableName, occurTimes, iteratorTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :param iteratorTimes:下级字段出现的次数，如果是0代表下级字段可以不出现，如果是1代表下级字段必须出现
        :return:该表全部数据  返回[{}]为异常缺失，返回0为正常缺失，其他返回正常数组
        '''
    try:
        table = data.get(tableName)
        if occurTimes == 0:
            if table is None or (table == "" and iteratorTimes == 0):
                return 0
            elif isinstance(table, list) is False or len(table) == 0 or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
        if occurTimes == 1:
            if table == "" and iteratorTimes == 0:
                return 0
            if table is None or isinstance(table, list) is False or len(table) == 0 \
                    or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
    except:
        return [{}]


def intCheck(str):
    try:
        int(str)
        return True
    except:
        return False


def past1MonQueryCnt():
    data = IC_DSJ_2D.get('data')
    # 查询记录概要 PQO[1.1,1]
    PQO = getTable(data, 'PQO', 1, 1)
    if PQO == [{}] or PQO == "":
        return u'缺失值'
    elif PQO == 0:
        return 0
    else:
        # 查询记录概要信息单元 PC05[1.1,1]
        PC05 = getTable(PQO[0], 'PC05', 1, 1)
        if PC05 == [{}] or PC05 == "":
            return u'缺失值'
        else:
            # 查询记录汇总信息段 PC05B[1.1,1]
            PC05B = getTable(PC05[0], 'PC05B', 1, 1)
            if PC05B == [{}]:
                return u'缺失值'
            elif PC05B == 0:
                return 0
            else:
                # 最近一个月内查询次数（贷款审批）
                PC05BS03 = PC05B[0].get("PC05BS03")
                # 最近一个月内查询次数（信用卡查询）
                PC05BS04 = PC05B[0].get("PC05BS04")
                if intCheck(PC05BS03) or intCheck(PC05BS04):
                    PC05BS03 = int(PC05BS03) if intCheck(PC05BS03) else 0
                    PC05BS04 = int(PC05BS04) if intCheck(PC05BS04) else 0
                    return PC05BS03 + PC05BS04
                else:
                    return u'缺失值'


if __name__ == "__main__":
    import json
    ratingRequest = {"legalPersonIdCard": "500101199201122461", "aa": "aa", "bb": ""}
    with open(r'C:\Users\zdr\Desktop\samples\IC_DSJ_2D.json') as ff:
        IC_DSJ_2D = json.loads(ff.read())
    aa = past1MonQueryCnt()
    print(aa)

result = past1MonQueryCnt()
